package top.hesisi.springboot3.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.hesisi.springboot3.base.PageBean;
import top.hesisi.springboot3.base.R;
import top.hesisi.springboot3.dao.TbGamePublisherMapper;
import top.hesisi.springboot3.entity.TbGamePublisher;
import top.hesisi.springboot3.service.TbGamePublisherService;
import top.hesisi.springboot3.vo.PageVO;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbGamePublisherServiceImpl implements TbGamePublisherService {
    private final TbGamePublisherMapper tbGamePublisherMapper;

    private TbGamePublisher queryByPublisher(String publisher) {
        QueryWrapper<TbGamePublisher> wrapper = new QueryWrapper<>();
        wrapper.eq("publisher", publisher);
        TbGamePublisher tbGamePublisher = tbGamePublisherMapper.selectOne(wrapper);
        return tbGamePublisher;
    }

    @Override
    public R<String> insert(TbGamePublisher tbGamePublisher) {
        TbGamePublisher check = queryByPublisher(tbGamePublisher.getPublisher());
        if (check != null) {
            return R.fail("游戏发行商已存在", null);
        }

        int i = tbGamePublisherMapper.insert(tbGamePublisher);
        return i==1 ? R.ok("添加成功", null) : R.fail("添加失败", null);
    }

    @Override
    public R<List<TbGamePublisher>> query(TbGamePublisher tbGamePublisher) {
        QueryWrapper<TbGamePublisher> wrapper = new QueryWrapper<>();
        wrapper.like(
                StringUtils.hasText(tbGamePublisher.getPublisher()),
                "publisher",
                tbGamePublisher.getPublisher()
        );
         wrapper.like(
                 StringUtils.hasText(tbGamePublisher.getIntro()),
                 "intro",
                 tbGamePublisher.getIntro()
         );
         List<TbGamePublisher> list = tbGamePublisherMapper.selectList(wrapper);
         return R.ok(list);
    }

    @Override
    public R<PageVO<TbGamePublisher>> queryPage(TbGamePublisher tbGamePublisher, PageBean pageBean) {
        IPage<TbGamePublisher> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        QueryWrapper<TbGamePublisher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("created");
        wrapper.like(
                StringUtils.hasText(tbGamePublisher.getPublisher()),
                "publisher",
                tbGamePublisher.getPublisher()
        );
        wrapper.like(
                StringUtils.hasText(tbGamePublisher.getIntro()),
                "intro",
                tbGamePublisher.getIntro()
        );
        page = tbGamePublisherMapper.selectPage(page,wrapper);
        return R.ok(page);
    }

    @Override
    public R<TbGamePublisher> queryById(Integer id) {
        TbGamePublisher tbGamePublisher = tbGamePublisherMapper.selectById(id);
        return tbGamePublisher == null ? R.fail("查询失败", null) : R.ok( tbGamePublisher);
    }

    @Override
    public R<String> update(TbGamePublisher tbGamePublisher) {
        TbGamePublisher check = queryByPublisher(tbGamePublisher.getPublisher());
        if (check != null && !check.getId().equals(tbGamePublisher.getId())) {
            return R.fail("游戏发行商已存在", null);
        }
        int i = tbGamePublisherMapper.updateById(tbGamePublisher);
        return i==1 ? R.ok("修改成功", null) : R.fail("修改失败", null);
    }

    @Override
    public R<String> delete(Integer id) {
        int i = tbGamePublisherMapper.deleteById(id);
        return i==1 ? R.ok("删除成功", null) : R.fail("删除失败", null);
    }
}
